package dao

import (
	"yundian/internel/app/models/orders"
	"yundian/internel/app/models/users"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// 抽单
func (d *OrderDao) GetTranOrderByDraw(dateStr string, userId int) *dingo.ErrorWrap {
	var lst []*orders.OrderModel
	sql := "SELECT * FROM orders WHERE is_draw = 1 and is_tianhai = 0 and fee_mode = 1 and state = 5 " +
		"and settlement_money >= 1000 and agent_user_id = ? and DATE_FORMAT(FROM_UNIXTIME(end_at),'%Y-%m-%d') = ?"
	db := global.Orm
	db.Raw(sql, userId, dateStr).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (d *OrderDao) CountTranOrderByDraw(dateStr string, userId int) *dingo.ErrorWrap {
	var count int64
	sql := "SELECT count(1) FROM orders WHERE is_draw = 1 and is_tianhai = 0 and fee_mode = 1 and state = 5 " +
		"and settlement_money >= 1000 and agent_user_id = ? and DATE_FORMAT(FROM_UNIXTIME(end_at),'%Y-%m-%d') = ?"
	db := global.Orm
	db.Raw(sql, userId, dateStr).Count(&count)
	return dingo.ErrorIf(db.Error, count)
}

// 未抽单
func (d *OrderDao) GetTranOrderByNoDraw(dateStr string) *dingo.ErrorWrap {
	var lst []*orders.OrderModel
	sql := "SELECT * FROM orders WHERE is_draw = 0 and is_tianhai = 0 and fee_mode = 1 and state = 5 " +
		"and settlement_money >= 1200 and DATE_FORMAT(FROM_UNIXTIME(end_at),'%Y-%m-%d') = ?"
	db := global.Orm
	db.Raw(sql, dateStr).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (d *OrderDao) CountTranOrderByNoDraw(dateStr string) *dingo.ErrorWrap {
	var count int64
	sql := "SELECT count(1) FROM orders WHERE is_draw = 0 and is_tianhai = 0 and fee_mode = 1 and state = 5 " +
		"and settlement_money >= 1200 and DATE_FORMAT(FROM_UNIXTIME(end_at),'%Y-%m-%d') = ?"
	db := global.Orm
	db.Raw(sql, dateStr).Count(&count)
	return dingo.ErrorIf(db.Error, count)
}

// 获取用户列表
func (d *UserDao) GetTranUsersByIsAdmin(isAdmin uint8) *dingo.ErrorWrap {
	var lst    []*users.UserIds

	db := global.Orm.Table("users").Select("id")
	db.Where("is_draw = 1") // 开启奖励
	db.Where("is_admin = ?", isAdmin)
	db.Find(&lst)

	return dingo.ErrorIf(db.Error, lst)
}